#include "classifiereditor.h"
#include "ui_classifiereditor.h"
#include "dataview/classifierproductmodel.h"
#include "database/dbtabclassifierproduct.h"

ClassifierEditor::ClassifierEditor(const QModelIndex &index,QWidget *parent) :
    BaseEditor(index,parent),
    ui(new Ui::ClassifierEditor)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    connect(this,SIGNAL(accepted()),this,SLOT(onAccept()));
    this->setGeometry(WINDOW_GEOMETRY_SHIFT(1,0.46,0));

    QGridLayout* glayout = new QGridLayout(this);
    for(int i =0;i<12;i++)
    {qDebug()<<i;
        RangeEditItem* item = new RangeEditItem(QString("R%1").arg(i+1),this);
        item->setObjectName(QString("R%1").arg(i+1));
        glayout->addWidget(item,i/4,i%4,1,1);
        layout()->setSpacing(2);
        layout()->setContentsMargins(QMargins(1,1,1,1));
        if(!mIsNew)
        {
            item->setLo(index.data(ClassifierProductModel::ROLE_R1_LOW+2*i).toFloat());
            item->setHi(index.data(ClassifierProductModel::ROLE_R1_HI+2*i).toFloat());
        }
    }
    ui->widget_range->setLayout(glayout);
    if(!mIsNew)
    {
        ui->le_name->setText(index.data(ClassifierProductModel::ROLE_NAME).toString());
        ui->le_barcode->setText(index.data(ClassifierProductModel::ROLE_BARCODE).toString());
    }
}

ClassifierEditor::~ClassifierEditor()
{
    delete ui;
}

float ClassifierEditor::getRangeLo(int rangeIndex)
{
    RangeEditItem* item = this->findChild<RangeEditItem*>(QString("R%1").arg(rangeIndex));
    if(item==nullptr) return 0;
    return item->getLo();
}

float ClassifierEditor::getRangeHi(int rangeIndex)
{
    RangeEditItem* item = this->findChild<RangeEditItem*>(QString("R%1").arg(rangeIndex));
    if(item==nullptr) return 0;
    return item->getHi();
}

Range ClassifierEditor::getRange(int rangeIndex)
{
    RangeEditItem* item = this->findChild<RangeEditItem*>(QString("R%1").arg(rangeIndex));
    return item->getRange();
}

void ClassifierEditor::on_btn_cancel_clicked()
{
    this->reject();
}

void ClassifierEditor::on_btn_ok_clicked()
{
    QString name = ui->le_name->text();
    QString barcode = ui->le_barcode->text();
    if(name.isEmpty())
    {
        MyMessageDialog::warning(0,"错误","请输入产品名称！");
        return;
    }

    DataBaseQuery query;
    bool accept =false;
    QMap<QString,QVariant> dataMap;
    dataMap.insert(DBTabClassifierProduct::_NAME,name);
    dataMap.insert(DBTabClassifierProduct::_BARCODE,barcode);
    for(int i = DBTabClassifierProduct::INDEX_R1_LOW;i<=DBTabClassifierProduct::INDEX_R12_LOW;i+=2)
    {
        dataMap.insert(DBTabClassifierProduct::getColumnName(i),getRangeLo((i-1)/2));//
    }

    for(int i = DBTabClassifierProduct::INDEX_R1_HI;i<=DBTabClassifierProduct::INDEX_R12_HI;i+=2)
    {
        dataMap.insert(DBTabClassifierProduct::getColumnName(i),getRangeHi((i-2)/2));
    }
    if(DBTabClassifierProduct::isExist(DBTabClassifierProduct::TAB_NAME,DBTabClassifierProduct::_NAME,name))
    {
        QString opt = QString(" \"%1\" = \"%2\"").arg(DBTabClassifierProduct::_NAME).arg(name);
        accept = query.updateItem(DBTabClassifierProduct::TAB_NAME,dataMap,opt);
    }
    else
    {
        accept = query.insert(DBTabClassifierProduct::TAB_NAME,dataMap);
    }
    if(accept)
        this->accept();
    else
        this->reject();
}

void ClassifierEditor::onAccept()
{
    emit updateData();
}
